package com.kfpanda.search.index.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.apache.lucene.document.Document;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

import com.kfpanda.search.index.AbstractFileDocument;

/**
 * <b>ClassName:<b> WordDocument
 * <br><b>Description: <br>将word文档转化成Document对象。
 * @Author: kfpanda
 * @Date：2012-1-6 下午10:58:56
 */
public class WordDocument extends AbstractFileDocument {
	
	/* (non-Javadoc)
	 * @see com.kfpanda.search.index.FileDocument#document(java.io.File)
	 
	@Override
	public Document document(File f) {
		// TODO Auto-generated method stub
		return null;
	}*/

	/* (non-Javadoc)
	 * @see com.kfpanda.search.index.FileDocument#fileReaderAll(java.lang.String, java.lang.String, int)
	 */
	@Override
	public String fileReaderAll(String filePath, String charset, int rowNum) {
		StringBuffer content = new StringBuffer("");// 文档内容
        try {
            HWPFDocument doc = new HWPFDocument(new FileInputStream(filePath));
            Range range = doc.getRange();
            int paragraphCount = range.numParagraphs();// 段落
            rowNum = rowNum < paragraphCount ? rowNum : paragraphCount;
            for (int i = 0; i < rowNum; i++) {// 遍历段落读取数据
                Paragraph pp = range.getParagraph(i);
                content.append(pp.text());
            }
        } catch (Exception e) {
        }
        return content.toString().trim();
	}
}
